package com.shop.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.Service.UserService;

import com.shop.Utils.MD5Utils;
import com.shop.Utils.TokenUtils;
import com.shop.dao.UserMapper;
import com.shop.mapper.GoodMapper;
import com.shop.mapper.UserImgMapper;
import com.shop.pojo.Message;
import com.shop.pojo.User;
import com.shop.pojo.UserImg;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

//    public UserServiceImpl() throws IOException {
//
//    }

    /**
     *  md5加密处理
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public User login(String username, String password) throws Exception {
//        return userDao.findByUsernameAndPassword(username,password);
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user =mapper.login(username);
        if (user!=null){
            if (MD5Utils.verify(password,user.getPassword())){
                return user;
            }
        }
        sqlSession.close();
        return null;
    }

    @Override
    public Message<String> addUser(String username, String password) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user =mapper.login(username);
        Message<String> message = new Message<String>();
        if (user==null){
            User addUser =new User(username,MD5Utils.md5(password));
            int i =mapper.addUser(addUser);
            if (i==1){
                sqlSession.commit();
                message.setMessage("添加成功");
                message.setData("添加成功");
            }else {
                message.setMessage("添加失败");
            }

            return message;
        }else {
            message.setMessage("用户名已经存在");
            return message;
        }
    }

    @Override
    public Message<String> TokenYan(String Authorization) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Message<String> message = new Message<String>();
        String[] strAry = TokenUtils.yanzhen(Authorization);
        if (strAry!=null){
            String username = strAry[0];
            String password = strAry[1];
            User user =mapper.login(username);
            if (user.getPassword().equals(password)){
                message.setStatus("200");
                return message;
            }
        }
        message.setStatus("401");
        message.setMessage("token无效");
        message.setData("token无效");
        sqlSession.close();
        return message;
    }

    @Override
    public Message<List<User>> selectAllUser(String queryInfo, int pageSize, int pageNum) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<User> userList = mapper.selectAllUser(queryInfo);
        Message<List<User>> message = new Message<>();
        message.setData(userList);
        sqlSession.close();
        return message;
    }

    @Override
    public Message<PageInfo<User>> selectAllUserPage(String queryInfo, int pageSize, int pageNum) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<User> userList = mapper.selectAllUser(queryInfo);
        Message<PageInfo<User>> message = new Message<>();
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        message.setData(pageInfo);
        return message;
    }

    @Override
    public Message<String> updatePassword(String username, String password) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(username,MD5Utils.md5(password));
        Message<String> message = new Message<>();
        int i =0;
        i = mapper.updatePassword(user);
        if (i>0){
            sqlSession.commit();
            message.setMessage("修改成功");
            message.setData("修改成功");
        }else {
            message.setMessage("修改失败");
        }



//         mapper.updatePassword(user);
//        sqlSession.commit();


        return message;
    }

    @Override
    public Message<String> deleteUser(String username) throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Message<String> message = new Message<>();
        int i= mapper.deleteUser(username);
        if (i>0){
            sqlSession.commit();
            message.setMessage("删除成功");
            message.setData("删除成功");
        }else {
            message.setMessage("删除失败");
        }
        return message;
    }

    @Override
    public Message<String> addImg(UserImg userImg) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserImgMapper mapper = sqlSession.getMapper(UserImgMapper.class);
        int i = mapper.addImg(userImg);
        if (i>0){
            sqlSession.commit();
            message.setMessage("添加成功");
            message.setData("添加成功");
        }else {
            message.setMessage("添加失败");
        }
        return message;
    }

    @Override
    public Message<String> deleteImg(String username) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserImgMapper mapper = sqlSession.getMapper(UserImgMapper.class);
        int i = mapper.deleteImg(username);
        if (i>0){
            sqlSession.commit();
            message.setMessage("删除成功");
            message.setData("删除成功");
        }else {
            message.setMessage("删除失败");
        }
        return message;
    }

    @Override
    public Message<String> selectImg(String username) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserImgMapper mapper = sqlSession.getMapper(UserImgMapper.class);
        String a = mapper.selectImg(username);
        message.setMessage("获取成功");
        message.setData(a);
        return message;
    }

    @Override
    public Message<String> updateImg(UserImg userImg) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserImgMapper mapper = sqlSession.getMapper(UserImgMapper.class);
        int i = mapper.updateImg(userImg);
        if (i>0){
            sqlSession.commit();
            message.setMessage("修改成功");
            message.setData("修改成功");
        }else {
            message.setMessage("修改失败");
        }
        return message;
    }
    @Override
    public Message<Integer> selectCount() throws Exception{
        Message<Integer> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int num = mapper.selectCount();
        message.setMessage("获取成功");
        message.setData(num);
        return message;
    }


}
